<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="订单编号" prop="sn">
                    <el-input class="w-[280px]" v-model="queryParams.sn" clearable placeholder="请输入订单编号" />
                </el-form-item>

                <el-form-item label="联系手机号" prop="phone">
                    <el-input class="w-[280px]" v-model="queryParams.phone" clearable placeholder="请输入联系手机号" />
                </el-form-item>
                <el-form-item label="房间名称" prop="room_name">
                    <el-select class="w-[280px]" v-model="queryParams.room_id" clearable placeholder="请选择房间名称">
                        <el-option v-for="item in roomList" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="订单编号" prop="sn" show-overflow-tooltip />
                    <el-table-column label="房间名称" prop="room_name" show-overflow-tooltip />
                    <el-table-column label="房间数" prop="number" show-overflow-tooltip />
                    <el-table-column label="支付金额" prop="order_amount" show-overflow-tooltip />
                    <el-table-column label="预定开始时间" prop="start_time" show-overflow-tooltip />
                    <el-table-column label="预定结束时间" prop="end_time" show-overflow-tooltip />
                    <el-table-column label="联系手机号" prop="phone" show-overflow-tooltip />
                    <el-table-column label="联系人" prop="name" show-overflow-tooltip />
                    <el-table-column label="订单状态" prop="status_txt" show-overflow-tooltip />

                    <el-table-column label="操作" width="80" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-if="row.status == 2"
                                type="danger"
                                link
                                @click="handleRefund(row)"
                            >
                                退款
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <!-- <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" /> -->
    </div>
</template>

<script lang="ts" setup name="orderLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiOrderLists, apiOrderRefund } from '@/api/order'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { apiRoomLists } from '@/api/room'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)


// 查询条件
const queryParams = reactive({
    sn: '',
    user_id: '',
    pay_sn: '',
    pay_way: '',
    pay_status: '',
    pay_time: '',
    order_amount: '',
    order_terminal: '',
    transaction_id: '',
    refund_status: '',
    refund_transaction_id: '',
    hotel_id: '',
    room_id: '',
    number: '',
    day: '',
    start_time: '',
    end_time: '',
    phone: '',
    name: '',
    status: '',
    room_name: '',
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiOrderLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 退款
const handleRefund = async (row: any) => {
    if (row.is_disable_refund == 1 && row.refund_status == 0) {
        const dialog = await feedback.formDialog({
            title: '请输入退款金额',
            formItems: [
                {
                    label: '退款金额',
                    prop: 'refund_amount',
                    type: 'input',
                }
            ],
            width: '460px',
            onSubmit: async (formData: any) => {
                try {
                    const res = await apiOrderRefund({ order_id: row.id, price: Number(formData.refund_amount) })
                    if(res.code == 1){
                        feedback.msgSuccess('退款成功')
                        dialog.close()
                        getLists()
                    }else{
                        feedback.msgError(res.msg)
                        dialog.close()
                    }
                } catch (error) {
                    console.error('退款失败:', error)
                    feedback.msgError('退款失败')
                    dialog.close()
                }
            },
            onCancel: () => {
                dialog.close()
            }
        })
    } else {
        feedback.msg('该订单不支持退款')
    }
}

getLists()

const roomList = ref<any[]>([])

onMounted(async () => {
    
})
</script>

